<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/4/21
 * Time: 15:11
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Report extends Base
{
    /**
     * 举报原因列表
     * @return mixed|\think\response\Json
     */
    public function reportReason(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('report_reason')->count();// 获取总条数
            $lists = Db::name('report_reason')
                ->field('rr_id, rr_name, rr_time')
                ->page($Nowpage,$limits)
                ->order('rr_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('report/reportreason');
    }
    
    /**
     * 添加举报原因
     * @return mixed|\think\response\Json
     */
    public function reasonAdd(){
        if(Request::instance()->isPost()){
            $insert = [
                'rr_name' => trim(input('rr_name'), ' '),
                'rr_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_report_reason')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('report/reasonadd');
    }
    
    /**
     * 编辑举报原因
     * @return mixed|\think\response\Json
     */
    public function reasonEdit(){
        $rr_id = input('rr_id');
        if(Request::instance()->isPost()){
            $res = Db::table('up_report_reason')
                ->where('rr_id', $rr_id)
                ->update(['rr_name' => trim(input('rr_name'), ' ')]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $reason = Db::table('up_report_reason')->where('rr_id', $rr_id)->value('rr_name');
        $this->assign([
            'rr_name' => $reason,
            'rr_id' => $rr_id
        ]);
        return $this->fetch('report/reasonedit');
    }
    
    /**
     * 删除举报原因
     * @return \think\response\Json
     */
    public function reasonDel(){
        $rr_id = input('id');
        $res = Db::table('up_report_reason')->delete($rr_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 举报列表
     * @return mixed|\think\response\Json
     */
    public function reportList(){
        if(request()->isAjax ()){
            $user_username = input('user_username');
            $user_phone = input('user_phone');
            $to_user_username = input('to_user_username');
            $report_type = input('report_type');
            $where['report_del'] = 1;
            if(isset($user_username) && $user_username != ''){
                $where['uuu1.user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['uuu1.user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($to_user_username) && $to_user_username != ''){
                $where['uuu2.user_username'] = ['like', '%' . $to_user_username . '%'];
            }
            if(isset($report_type) && $report_type != ''){
                $where['report_type'] = $report_type;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('report')
                ->join('up_u_user uuu1', 'uuu1.user_id = report_user_id', 'left')
                ->join('up_u_user uuu2', 'uuu2.user_id = report_to_user_id', 'left')
                ->join('up_information', 'info_id = report_info_id', 'left')
                ->join('up_information_comment', 'comment_id = report_comment_id', 'left')
                ->join('up_information_reply', 'reply_id = report_reply_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('report')
                ->join('up_u_user uuu1', 'uuu1.user_id = report_user_id', 'left')
                ->join('up_u_user uuu2', 'uuu2.user_id = report_to_user_id', 'left')
                ->join('up_information', 'info_id = report_info_id', 'left')
                ->join('up_information_comment', 'comment_id = report_comment_id', 'left')
                ->join('up_information_reply', 'reply_id = report_reply_id', 'left')
                ->where($where)
                ->field('uuu1.user_username, uuu1.user_phone, uuu1.user_create_id,
                uuu2.user_username to_user_name, uuu2.user_phone to_user_phone, report_id,
                report_status, report_type, report_reason, report_time, comment_content,
                reply_content, info_id, report_reason_id')
                ->page($Nowpage,$limits)
                ->order('report_time desc')
                ->select();
            if($lists){
                foreach($lists as $k => $v){
                    if($v['report_reason_id'] == 0){
                        $lists[$k]['report_reason'] = '其它';
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('report/reportlist');
    }
    
    /**
     * 举报详情
     * @return mixed
     */
    public function reportDetail(){
        $report_id = input('report_id');
        $detail = Db::table('up_report')
            ->join('up_u_user uuu1', 'uuu1.user_id = report_user_id', 'left')
            ->join('up_u_user uuu2', 'uuu2.user_id = report_to_user_id', 'left')
            ->field('uuu1.user_username, uuu1.user_phone, uuu1.user_create_id,
                uuu2.user_username to_user_name, uuu2.user_phone to_user_phone, report_status, 
                report_type, report_reason, report_time, report_info_id, report_comment_id, report_reply_id')
            ->where('report_id', $report_id)
            ->find();
        // 风讯
        if($detail['report_type'] == 1){
            $info = Db::table('up_information')
                ->where('info_id', $detail['report_info_id'])
                ->field('info_content, info_images, info_type')
                ->find();
            if($info['info_type'] == 2){
                if(!empty($info['info_images'])){
                    $info['info_images'] = explode(',', trim($info['info_images'], ','));
                }
            }
            $detail['info_content'] = $info['info_content'];
            $detail['info_images'] = $info['info_images'];
            $detail['info_type'] = $info['info_type'];
        }
        // 评论
        if($detail['report_type'] == 2){
            $detail['comment_content'] = Db::table('up_information_comment')
                ->where('comment_id', $detail['report_comment_id'])
                ->value('comment_content');
        }
        // 回复
        if($detail['report_type'] == 3){
            $detail['report_content'] = Db::table('up_information_reply')
                ->where('reply_id', $detail['report_reply_id'])
                ->value('reply_content');
        }
        return $this->fetch('report/reportdetail', ['detail' => $detail]);
    }
    
    /**
     * 标为已读
     * @return \think\response\Json
     */
    public function reportReaded(){
        $report_id = input('id');
        $res = Db::table('up_report')->where('report_id', $report_id)->setField('report_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除举报记录
     * @return \think\response\Json
     */
    public function reportDel(){
        $report_id = input('id');
        $res = Db::table('up_report')->where('report_id', $report_id)->setField('report_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}